package demo2;

import java.util.Arrays;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: xiaotutu
 * Date: 2024-03-17
 * Time: 13:46
 */
public class SeqList {
    private int[] elem;

    private int usedSize;

    private static final int DEFAULT_CAPACITY = 10;

    public SeqList() {
        this.elem = new int[DEFAULT_CAPACITY];
    }

    // 打印顺序表，注意：该方法并不是顺序表中的方法，
    public void display() {
        for (int i = 0; i < this.usedSize; i++) {
            System.out.print(this.elem[i] + " ");
        }
        System.out.println();
    }

    // 新增元素,默认在数组最后新增
    public void add(int data) {
        // 如果满了的话 怎么办?
        if(isFull()) {
           resize();
        }
        this.elem[usedSize] = data;
        this.usedSize++;
    }

    private void resize() {
        elem = Arrays.copyOf(elem,elem.length*2);
    }

    // 判定是否包含某个元素
    public boolean contains(int toFind) {
        for (int i = 0; i < this.usedSize; i++) {
            if(toFind == elem[i]) {
                return true;
            }
        }
        return false;
    }
    // 查找某个元素对应的下标
    public int indexOf(int toFind) {
        for (int i = 0; i < this.usedSize; i++) {
            if(toFind == elem[i]) {
                return i;
            }
        }
        return -1;
    }
    // 获取顺序表长度
    public int size() {
        return this.usedSize;
    }

    // 获取 pos 位置的元素
    public int get(int pos) {
        if (!checkPos(pos)) {
            throw new PosOutBoundsException("get 获取元素时,位置不合法");
        }
        return elem[pos];
    }

    // 给 pos 位置的元素设为 value (更新的意思)
    public void set(int pos, int value) {
        if(!checkPos(pos)) {
            throw new PosOutBoundsException("set 设置元素时, 位置不合法!");
        }

        this.elem[pos] = value;
    }

    private boolean checkPos(int pos) {
        if(pos < 0 || pos >= usedSize) {
            return false;
        }
        return true;
    }

    private boolean isFull() {
        if(this.usedSize == elem.length) {
            return true;
        }
        return false;
    }

    // 在 pos 位置新增元素
    public void add(int pos, int data) {
        if(pos < 0 || pos > usedSize) {
            throw new PosOutBoundsException("add 新增元素时,位置不合法!");
        }
        if(isFull()) {
            resize();
        }
        for (int i = usedSize-1; i >= pos ; i--) {
            this.elem[i+1] = this.elem[i];
        }
        this.elem[pos] = data;
        this.usedSize++;
    }

    private boolean isEmpty() {
        return this.usedSize == 0;
    }

    //删除第一次出现的关键字key
    public void remove(int toRemove) {
        if(isEmpty()) {
            return;
        }
        int index = indexOf(toRemove);
        if(index == -1) {
            return;
        }

        for (int i = index+1; i < this.usedSize; i++) {
            this.elem[i-1] = this.elem[i];
        }
        this.usedSize--;

    }

    // 清空顺序表
    public void clear() {
        /*for (int i = 0; i < elem.length; i++) {
            this.elem[i] = null;
        }*/
        this.usedSize = 0;
    }
}
